/*
 * OpenFileAction.java
 *
 * Created on March 7, 2005, 11:29 AM
 */
package com.bff.mpd.musicjukebox.lib.actions;

import com.bff.mpd.utils.Utils;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

/**
 *
 * @author findeisb
 */
public class OpenDirAction extends AbstractAction {

    private final ImageIcon openIcon = new ImageIcon(getClass().getResource("/resources/mpd/open.png"));
    private File currentDirectory;
    private JFrame frame;
    private JFileChooser fc;
    private JTextField textField;

    public OpenDirAction(JFrame frame, JTextField textField) {
        super(Utils.getMessage("ACTION_OPEN_NAME"));
        this.frame = frame;
        this.textField = textField;
        this.fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        putValue(Action.SMALL_ICON, openIcon);
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {

        int returnVal = fc.showOpenDialog(frame);

        //Query the JFileChooser to get the input from the user
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            textField.setText(fc.getSelectedFile().getPath());
            currentDirectory = fc.getCurrentDirectory();
            fc.setCurrentDirectory(currentDirectory);
        }
    }
};
